package org.haptimap.examples;

import java.util.List;

import org.haptimap.hcimodules.touchovermap.Line;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 * LinesOverlay draws lines on the canvas using the projection of the given MapView.
 */
public class LinesOverlay extends Overlay {

	List<Line> lines;
	
    public LinesOverlay(List<Line> lines){
    	this.lines = lines;
    }   
    
    public void draw(Canvas canvas, MapView mapv, boolean shadow){
        super.draw(canvas, mapv, shadow);
        
        // draw() is called twice: Once to draw the actual lines and a second time to draw the lines shadow
        // We won't draw a shadow for lines so just skip this call when a shadow is requested
        if (shadow)
        	return;
        
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(6);
        
        Projection projection = mapv.getProjection();
        Point p = new Point();
        
		for (Line s : this.lines) {

			Path path = new Path();
			
			boolean firstElement = true;

			// draw lines
			for (Location l : s.getElements()) {
				if (l.getProvider().equals("way")){				
					if (firstElement) {
						// first element needs move to
						projection.toPixels(new GeoPoint((int) (l.getLatitude() * 1E6),	(int) (l.getLongitude() * 1E6)), p);
						path.moveTo(p.x, p.y);
						firstElement = false;
					}
					// others will draw lines
					projection.toPixels(new GeoPoint((int) (l.getLatitude() * 1E6),	(int) (l.getLongitude() * 1E6)), p);
					path.lineTo(p.x, p.y);
				}
			}
			
			canvas.drawPath(path, paint);
		}
    }
}